// import { Message } from 'element-ui'
// 导出一个axios的实例

import axios from 'axios'
import store from '@/store'
const service = axios.create({
  baseURL: 'http://likede2-admin.itheima.net/likede',
  timeout: 5000
}) // 创建一个axios的实例
const Timeout = 3
function isCheckTimeOut() {
  const nowTime = Date.now()
  const timeStamp = (nowTime - store.getters.dikedeTime) / 1000
  return timeStamp > Timeout
}
service.interceptors.request.use(config => {
  console.log('拦截器', config)
  if (store.getters.token) {
    if (isCheckTimeOut()) {
      this.$store.dispatch('user/layoutAction')
      this.$router.push('/login')
      return Promise.reject(new Error('token过期了，请重新登录'))
    }
    config.headers.Authorization = store.getters.token
  }
  return config
}, error => {
  return Promise.reject(error)
})

// service.interceptors.response.use(response => {
//   const { data, msg, success } = response.data
//   if (success) {
//     return data // 里面的token值
//   } else {
//     Message.error(msg)
//     return Promise.reject(new Error(msg))
//   }
// }, error => {
//   return Promise.reject(error)
// })
export default service // 导出axios实例
